import __builtin__
from client.bulletsystem.testbullet import TestBullet
from direct.showbase.DirectObject import DirectObject
from panda3d.core import Vec3, VBase3


class Tower(object, DirectObject):

    def __init__(self, name, node, hp, rng, atkspd, damage, radius, cost, upgrades):
        self.name = name
        self.towerNode = node
        self.upgrades = upgrades
        self.cost = cost
        self.hitpoints = hp
        self.range = rng
        self.attackspeed = atkspd
        self.timeSinceLastShot = atkspd
        self.curTarget = 0
        self.radius = radius
        self.damage = damage

    def get_nearest_from_list(self, list):
        nearest = 0
        for e in list:
            e.distance = VBase3.length(self.towerNode.getPos() - e.unit_object.getPos())
            if e.distance < self.range:
                if nearest == 0 or e.distance < nearest.distance:
                    nearest = e

        return nearest

    def destroy(self):
        self.towerNode.remove_node()

    def getposition(self):
        return self.towerNode.getPos()

    def shoot(self):
        # print(self.name + " shoots at " + str(self.curTarget))
        __builtin__.base.gamestatemanager.getState("RunningSession").bulletmanager.create_bullet("TestBullet", Vec3(self.towerNode.getX(), self.towerNode.getY(), self.towerNode.getZ() + 2), self.curTarget, self.damage)